package com.frost.demo.ioc;

import com.frost.demo.ioc.component.UserComponent;
import com.frost.demo.ioc.config.UserConfig;
import com.frost.demo.ioc.controller.UserController;
import com.frost.demo.ioc.model.UserInfo;
import com.frost.demo.ioc.repo.UserRepository;
import com.frost.demo.ioc.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.beans.Introspector;

@SpringBootApplication
public class IocDemo1Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(IocDemo1Application.class, args);
        // 根据类型获取bean
        UserController bean = context.getBean(UserController.class);
//        UserController bean = new UserController(new UserService());
        bean.sayHi();
    }

    public static void main1(String[] args) {
        ApplicationContext context = SpringApplication.run(IocDemo1Application.class, args);
        // 根据类型获取bean
//        UserController bean = context.getBean(UserController.class);
//        bean.sayHi();
//
//        // 根据名称获取bean
//        UserService userService = (UserService) context.getBean("userService");
//        userService.sayHi();
//
//        // 根据名称和类型获取bean
//        UserComponent userComponent = context.getBean("userComponent", UserComponent.class);
//        userComponent.sayHi();
//
//        UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
//        userRepository.sayHi();

//        UserConfig userConfig = (UserConfig) context.getBean("userConfig");
//        userConfig.sayHi();

//        UserInfo userInfo = context.getBean("userInfo2", UserInfo.class);
//        System.out.println(userInfo);

        // 获取bean的名称
//        System.out.println(Introspector.decapitalize("UController"));
//        System.out.println(Introspector.decapitalize("userController"));
//        System.out.println(Introspector.decapitalize("UserController"));

        UserInfo user1 = (UserInfo) context.getBean("user1");
        System.out.println(user1);
    }
}

